package web

import (
	"DeviceManager/logger"
	"log"
	"net/http"
)

func StartServer(port string) {
	// 打印即将使用的Web端口
	// log.Printf("Starting server on port: %s", config.Cfg.WebPort)
	logger.LogMessage("INFO", "Starting server on port: "+port)

	// 路由设置
	http.HandleFunc("/", homeHandler)
	http.HandleFunc("/login", loginHandler)
	http.HandleFunc("/config", configHandler)
	http.HandleFunc("/devices", deviceHandler)

	// 静态文件处理
	fs := http.FileServer(http.Dir("web/static"))
	http.Handle("/static/", http.StripPrefix("/static/", fs))

	// 启动服务器
	err := http.ListenAndServe(":"+port, nil)
	if err != nil {
		log.Fatal("ListenAndServe: ", err)
	}
}

func homeHandler(w http.ResponseWriter, r *http.Request) {
	http.Redirect(w, r, "/static/login.html", http.StatusSeeOther)
}
